package jdk.optional;

import jdk.entity.UserEntity;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author songyafei
 */
public class OptionalTest {

    public static void main(String[] args) {
        Optional<UserEntity> userEntity1 = Optional.of(new UserEntity(1L, 18, "zhang", LocalDate.now()));
        Optional<UserEntity> userEntity2 = Optional.of(new UserEntity(2L, 10, "li", LocalDate.now().minusYears(1)));
        Optional<UserEntity> userEntity3 = Optional.of(new UserEntity(3L, 20, "wang", LocalDate.now().minusYears(3)));
        UserEntity userEntity4 = new UserEntity(4L, 21, "song", LocalDate.now().minusYears(4));
        UserEntity userEntity5 = new UserEntity(5L, 32, "hao", LocalDate.now().minusYears(2));
        Optional<UserEntity> userEntity10 = Optional.empty();
        UserEntity userEntity6 = new UserEntity();
        UserEntity userEntity7 = null;

        Optional<List<UserEntity>> userEntityList = Optional.ofNullable(Arrays.asList(userEntity4,userEntity5));


        Optional<UserEntity> userEntity61 = Optional.ofNullable(userEntity6);
        Optional<UserEntity> userEntity71 = Optional.ofNullable(userEntity7);


        System.out.println(userEntity71.get());
        System.out.println(userEntity71.isPresent());


        userEntity1.ifPresent(m-> System.out.println(m.getAge()));

        userEntity1.ifPresentOrElse(m-> {
            System.out.println(m.getAge()+1);
        },()-> System.out.println(""));

        userEntity1.filter(m->m.getAge()>19).ifPresentOrElse(m-> {
            System.out.println(m.getAge()*19);
        },()-> System.out.println("不大于"));

        Optional<UserEntity> userEntity = Optional.ofNullable(new UserEntity(1L, 77, "wang bo", LocalDate.now()));
        System.out.println(userEntity.get().getName());


        if(userEntityList.isPresent()){
            List<String> nameList= new ArrayList<>();
            userEntityList.ifPresentOrElse(m->{
                List<String> collect = m.parallelStream().filter(i -> i.getAge() > 18).map(UserEntity::getName).collect(Collectors.toList());
                nameList.addAll(collect);
            },()-> System.out.println("数据为空"));
            System.out.println("获取姓名集合"+nameList.toString());
        }

    }
}
